﻿using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using xiushen.Domain;
using xiushen.Infrastructure;

namespace xiushen.Tests
{
    public class XiushenRepository:Repository<Category>
    {
         public Dictionary<string,int> InsertCategory(string[] levels,int ?parentId)
         {
             var categoryId = QueryAll().Max(r => r.ID);
             var category = new Dictionary<string, int>();
             for (int i = 0; i < levels.Length; i++)
             {
                 var data = new Category() { ID = ++categoryId, Name = levels[i],ParentID = parentId};
                 category.Add(data.Name,data.ID);
                 var entities = new Entities();
                 entities.AddToCategory(data);
                 entities.SaveChanges();
             }
             return category;

         }

        public int InsertBook(Book book)
        {

            int bookId = 0;
            if (QueryAll<Book>().Count() > 0)
            {
                bookId = QueryAll<Book>().Max(r => r.ID);
            }
            book.ID = ++bookId;
        
            Insert(book);
            return bookId;
        }
    }

    /// <summary>
    /// Summary description for DataInsert
    /// </summary>
    [TestClass]
    public class DataInsert
    {
        public DataInsert()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            //var data = new Category() {ID = 1, Name = "国学"};
            //var entities = new Entities();
            //entities.AddToCategory(data);
            //entities.SaveChanges();
            var saveRepositry = new XiushenRepository();

            var sub = "经部 史部 子部 集部 诗词散文 小说传奇 国学入门 书目辞典 国学大家 国学普及读物 综合";
           var vlues = sub.Split(' ');
            int i = 2;
            foreach (var vlue in vlues)
            {
                var data = new Category() {ID = i, Name = vlue,ParentID = 1};
                saveRepositry.Insert(data);
                ++i;
            }

        }
        [TestMethod]
        public void TestMethod2()
        {

            var data = new Category() { ID = 69, Name = "文言文名篇"};
            var entities = new Entities();
            entities.AddToCategory(data);
            entities.SaveChanges();
        


        }
        [TestMethod]
        public void TestMethod3()
        {
            //var data = new Category() { ID = 13, Name = "佛学" };
            //var entities = new Entities();
            //entities.AddToCategory(data);
            //entities.SaveChanges();
            var saveRepositry = new XiushenRepository();
            var sub = "小学部分文言文 初中部分文言文 高中部分文言文 古文观止文言文译文 菜根谭文言文译文";
            var vlues = sub.Split(' ');
            int i = 70;
            foreach (var vlue in vlues)
            {
                var data = new Category() { ID = i, Name = vlue, ParentID = 69 };
                saveRepositry.Insert(data);
                ++i;
            }

        }
        [TestMethod]
        public void DynamicInsert()
        {
             var saveRepositry = new XiushenRepository();
              var levels = new[] {"中医养生", "少儿启蒙"};
             var categoryId = saveRepositry.QueryAll().Max(r => r.ID);
             for (int i = 0; i < levels.Length; i++)
            {
                var data = new Category() { ID = ++categoryId , Name = levels[i] };
                var entities = new Entities();
                entities.AddToCategory(data);
                entities.SaveChanges();
            }
           
            
        }
    }
}
